home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / ImageIcon.java < prev    next >
Text File  |  1998-10-03  |  5KB  |  214 lines

  1. package com.symantec.itools.swing.icons;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.*;
  5. import java.net.URL;
  6. import com.sun.java.swing.*;
  7. import java.io.Serializable;
  8. import java.io.InputStream;
  9. import java.io.ByteArrayOutputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.ObjectInputStream;
  12. import java.io.IOException;
  13.  
  14. public class ImageIcon
  15.     implements Icon, java.io.Serializable
  16. {    
  17.     /**
  18.      * Creates an uninitialized image icon.
  19.      */
  20.     public ImageIcon()
  21.     {
  22.     }
  23.     
  24.     /**
  25.      * Creates an image icon from the specified URL. The image will
  26.      * be preloaded by using MediaTracker to monitor the loaded state
  27.      * of the image.
  28.      */
  29.     public ImageIcon (URL location)
  30.     {
  31.         imageLocation = location;
  32.         image = Toolkit.getDefaultToolkit().getImage(location);
  33.         loadImage(image);
  34.     }
  35.     
  36.     /**
  37.      * Creates an image icon from the specified Image. The image will
  38.      * be preloaded by using MediaTracker to monitor the loaded state
  39.      * of the image.
  40.      */
  41.     public ImageIcon (Image i)
  42.     {
  43.         image = i;
  44.         loadImage(image);
  45.     }
  46.     
  47.     /**
  48.      * Creates an image icon from the specified InputStream. The image will
  49.      * be preloaded by using MediaTracker to monitor the loaded state
  50.      * of the image.  The InputStream is closed when the read is done.
  51.      */
  52.     public ImageIcon (InputStream inStream)
  53.         throws IOException
  54.     {
  55.         int                   numRead;
  56.         byte[]                bytes;
  57.         ByteArrayOutputStream outStream;
  58.         
  59.         bytes     = new byte[1024];
  60.         outStream = new ByteArrayOutputStream();
  61.         
  62.         while((numRead = inStream.read(bytes)) > 0)
  63.         {
  64.             outStream.write(bytes, 0, numRead);
  65.         }
  66.         
  67.         inStream.close();
  68.         outStream.flush();
  69.         
  70.         image = Toolkit.getDefaultToolkit().createImage(outStream.toByteArray());
  71.         loadImage(image);
  72.         outStream.close();
  73.     }
  74.     
  75.     //
  76.     // Properties
  77.     //
  78.     
  79.     /**
  80.      * Returns the location of the Image displayed by this icon.
  81.      */
  82.     public URL getImageLocation ()
  83.     {
  84.         return imageLocation;
  85.     }
  86.     
  87.     /**
  88.      * Set the location to displayed by this icon. The image will
  89.      * be preloaded by using MediaTracker to monitor the loading state
  90.      * of the image.
  91.      */
  92.     public void setImageLocation (URL location)
  93.     {
  94.         imageLocation = location;
  95.         image = Toolkit.getDefaultToolkit().getImage(location);
  96.         loadImage(image);
  97.     }
  98.     
  99.     /** 
  100.      * Set the image observer for the image.  Set this
  101.      * property if the ImageIcon contains an animated GIF.
  102.      * For example:
  103.      * <pre>
  104.      *     icon = new ImageIcon(...)
  105.      *     button.setImageLocation(URL);
  106.      *     icon.setImageObserver(button);
  107.      * </pre>
  108.      */
  109.     public void setImageObserver(ImageObserver observer)
  110.     {
  111.         imageObserver = observer;
  112.     }
  113.     
  114.     /**
  115.      *  Return the image observer for the image 
  116.      */
  117.     public ImageObserver getImageObserver()
  118.     {
  119.         return imageObserver;
  120.     }
  121.     
  122.     //
  123.     // Icon interface implementation
  124.     //
  125.     
  126.     /**
  127.      * Paints the Icon
  128.      */
  129.     public synchronized void paintIcon(Component c, Graphics g, int x, int y)
  130.     {
  131.         if (image != null)
  132.             if (imageObserver == null)
  133.                 g.drawImage(image, x, y, c);
  134.             else
  135.                 g.drawImage(image, x, y, imageObserver);
  136.     }
  137.     
  138.     /**
  139.      * Get the width of the Icon
  140.      */
  141.     public int getIconWidth()
  142.     {
  143.         return width;
  144.     }
  145.     
  146.     /**
  147.      * Get the height of the Icon
  148.      */
  149.     public int getIconHeight()
  150.     {
  151.         return height;
  152.     }
  153.     
  154.     //
  155.     // Implementation
  156.     //
  157.     
  158.     /**
  159.      * Wait for the image to load
  160.      */
  161.     protected void loadImage(Image image)
  162.     {
  163.         synchronized(tracker)
  164.         {
  165.             tracker.addImage(image, 0);
  166.             
  167.             try
  168.             {
  169.                 tracker.waitForID(0, 5000);
  170.             }
  171.             catch (InterruptedException e)
  172.             {
  173.                 System.out.println("INTERRUPTED while loading Image");
  174.             }
  175.             
  176.             loadStatus = tracker.statusID(0, false);
  177.             
  178.             tracker.removeImage(image, 0);
  179.             
  180.             width = image.getWidth(imageObserver);
  181.             height = image.getHeight(imageObserver);
  182.         }
  183.     }
  184.     
  185.     /**
  186.      * Returns the status of the image loading operation.
  187.      * @return the loading status as defined by java.awt.MediaTracker.
  188.      * @see java.awt.MediaTracker#ABORTED
  189.      * @see java.awt.MediaTracker#ERRORED
  190.      * @see java.awt.MediaTracker#COMPLETE
  191.      */
  192.     public int getImageLoadStatus()
  193.     {
  194.         return loadStatus;
  195.     }
  196.     
  197.     public Image getImage()
  198.     {
  199.         return image;
  200.     }
  201.     
  202.     protected transient Image image;
  203.     protected transient int loadStatus = 0;
  204.     protected ImageObserver imageObserver;
  205.     protected URL imageLocation = null;
  206.     
  207.     protected final static Component component = new Component() {};
  208.     protected final static MediaTracker tracker = new MediaTracker(component);
  209.     
  210.     protected int width = -1;
  211.     protected int height = -1;
  212. }
  213.  
  214.